package tool.zyk.consts;

import lombok.AllArgsConstructor;
import lombok.Getter;
import tool.zyk.dto.SerializableDTO;

import java.util.Arrays;
import java.util.Objects;

/**
 * author: zyk
 * datetime: 2024/1/27 16:11
 * describe: 序列化类型
 */

@Getter
@AllArgsConstructor
public enum SerializableTypeEnum {
    JSON(1, "json", "json方式"),
    XML(2, "xml", "xml"),
    JAVA_BYTE(3, "java_byte", "java二进制序列化方式"),
    ;

    private final Integer code;
    private final String type;
    private final String desc;

    public static SerializableTypeEnum findByCode(Integer code) {
        return Arrays.stream(values())
                .filter(a -> Objects.equals(code, a.getCode()))
                .findFirst()
                .orElseThrow(() -> new RuntimeException("未知的序列化类型"));
    }

    public SerializableDTO ofDTO() {
        SerializableDTO serializableDTO = new SerializableDTO();
        serializableDTO.setCode(this.getCode());
        serializableDTO.setDesc(this.getDesc());
        return serializableDTO;
    }
}
